# !usr/bin/python
# -*-coding: utf-8-*-
# Filename: singlelistreverse.py

# 单链表
class SingleList:

	_Center = None
	_Next = None

	def __init__( self, center = 0 ):
		self._Center = center

	def setNext( self, next ):
		self._Next = next

# 单链表打印函数
def printSingleList( sl ):
	if( sl != None ):
		print( sl._Center )
		printSingleList( sl._Next )

# 单链表逆序打印函数
def printSingleListReverse( sl ):
	if( sl == None ):
		return

	if( sl._Next != None ):
		printSingleListReverse( sl._Next )
		
	print( sl._Center )

# 单链表逆序函数
def ReverseSingle( sl ):
	tmpnode = None
	tmpNow = None
	tmpNext = None

	tmpnode = sl
	if( tmpnode == None ):
		return

	while( tmpnode._Next != None ):
		tmpNext = tmpnode._Next
		tmpnode._Next = tmpNow
		tmpNow = tmpnode
		tmpnode = tmpNext

	tmpnode._Next = tmpNow
	# 返回逆序后的头
	return tmpnode


head = SingleList( 0 )
n1 = SingleList( 1 )
n2 = SingleList( 2 )
n3 = SingleList( 3 )
n4 = SingleList( 4 )
n5 = SingleList( 5 )
n6 = SingleList( 6 )
head.setNext( n1 )
n1.setNext( n2 )
n2.setNext( n3 )
n3.setNext( n4 )
n4.setNext( n5 )
n5.setNext( n6 )





printSingleList( head )
printSingleListReverse( head )

now = ReverseSingle( head )

printSingleList( now )
printSingleListReverse( now )